package com.lib.utls.pop;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lib.base.R;
import com.lib.utls.glide.GlideConfig;
import com.luck.picture.lib.tools.ScreenUtils;
import com.zyyoona7.popup.EasyPopup;

import androidx.cardview.widget.CardView;


public class SmallZoomImagePopupUtils {


    private Context context;


    public SmallZoomImagePopupUtils(Context context) {

        this.context = context;

        initPopuPopu();
    }

    private EasyPopup popu;


    private void initPopuPopu() {
        popu = EasyPopup.create()
                .setContext(context)
                .setContentView(R.layout.popup_small_zoom_image)
                .setWidth(WindowManager.LayoutParams.FILL_PARENT)
                .setHeight(WindowManager.LayoutParams.FILL_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initView(view);


                    }

                })
                .setFocusAndOutsideEnable(true)
                .apply();


    }

    public void setZoomImage(String url) {
        Glide.with(context)
                .load(url)
                .apply(GlideConfig.getRectangleOptions())
                .into(iv_photo);
    }


    private ImageView iv_photo;

    private ImageView iv_delete;

    private CardView card;

    private void initView(View view) {
        iv_photo = view.findViewById(R.id.iv_photo);
        iv_delete = view.findViewById(R.id.iv_delete);

        card = view.findViewById(R.id.card);


        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) card.getLayoutParams();


        int margin = ScreenUtils.dip2px(context, 10);
        int cut = ScreenUtils.dip2px(context, 120);
        int width = ScreenUtils.getScreenWidth(context);
        int height = ScreenUtils.getScreenHeight(context);
        // int minSize = width > height ? height : width;


        if (width > height) {
            //横屏

            params.width = height + cut;
            params.height = height - margin * 2;
        } else {
            //竖屏
            params.width = width - margin * 2;
            params.height = width - cut;
        }


        //params.setMargins(margin, margin, margin, margin);

        card.setLayoutParams(params);


        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }


    private void showToast(String a) {
        Toast.makeText(context, a, Toast.LENGTH_SHORT).show();
    }


    public void dismiss() {
        popu.dismiss();

    }


    public void showAnswerPopuPopu(View view) {


        popu.showAtLocation(view, Gravity.CENTER, 0, 0);
    }


}
